
**** Create data on fintech loans

******************************************************************	
** Figure H.10: Adoption dynamics for other tech based on distance to hub
******************************************************************	

use "$datadir\ewallet\maindata.dta", clear
	label var postXdist500 " $ \text{(Distance to hub)}_{d} $ $ \times $ Post"
	
	forval i = 2/5{
		local j = `i'*0.1
		g min_distance_hub_500`i' = (min_distance_hub_500 > `j')
		g post`i'Xdist500 = post*min_distance_hub_500`i'
	}
	
	forval i=1/14{
		replace timeid`i'Xdist500 = (time_id == 28+`i')*min_distance_hub_5004
	}
	replace timeid6Xdist500 = 0

	**** Event study: Mobile wallet
	statsby eff_1=_b[timeid1Xdist500] eff_2=_b[timeid2Xdist500] eff_3=_b[timeid3Xdist500] eff_4=_b[timeid4Xdist500] ///
	eff_5=_b[timeid5Xdist500] eff_6=_b[timeid6Xdist500] eff_7=_b[timeid7Xdist500] eff_8 =_b[timeid8Xdist500] ///
	eff_9 =_b[timeid9Xdist500] eff_10 =_b[timeid10Xdist500] eff_11=_b[timeid11Xdist500] eff_12 =_b[timeid12Xdist500] ///
	eff_13 =_b[timeid13Xdist500] eff_14 =_b[timeid14Xdist500] se_1 =_se[timeid1Xdist500] se_2 =_se[timeid2Xdist500] ///
	se_3 = _se[timeid3Xdist500] se_4 =_se[timeid4Xdist500] se_5 =_se[timeid5Xdist500] se_6 =_se[timeid6Xdist500] se_7 =_se[timeid7Xdist500] ///
	se_8 =_se[timeid8Xdist500] se_9 =_se[timeid9Xdist500] se_10 =_se[timeid10Xdist500] se_11 =_se[timeid11Xdist500] se_12 =_se[timeid12Xdist500] ///
	se_13 =_se[timeid13Xdist500] se_14 =_se[timeid14Xdist500] , ///
	saving("$datadir\mobwalletprop_500.dta", replace): ///
	reghdfe lognewfirms timeid*Xdist500 if $filters, a($fe $dist_controls##i.time_id i.state_id#i.time_id) cluster(district_id)
	
	**** Event study: fintech loans
	statsby eff_1=_b[timeid1Xdist500] eff_2=_b[timeid2Xdist500] eff_3=_b[timeid3Xdist500] eff_4=_b[timeid4Xdist500] ///
	eff_5=_b[timeid5Xdist500] eff_6=_b[timeid6Xdist500] eff_7=_b[timeid7Xdist500] eff_8 =_b[timeid8Xdist500] ///
	eff_9 =_b[timeid9Xdist500] eff_10 =_b[timeid10Xdist500] eff_11=_b[timeid11Xdist500] eff_12 =_b[timeid12Xdist500] ///
	eff_13 =_b[timeid13Xdist500] eff_14 =_b[timeid14Xdist500] se_1 =_se[timeid1Xdist500] se_2 =_se[timeid2Xdist500] ///
	se_3 = _se[timeid3Xdist500] se_4 =_se[timeid4Xdist500] se_5 =_se[timeid5Xdist500] se_6 =_se[timeid6Xdist500] se_7 =_se[timeid7Xdist500] ///
	se_8 =_se[timeid8Xdist500] se_9 =_se[timeid9Xdist500] se_10 =_se[timeid10Xdist500] se_11 =_se[timeid11Xdist500] se_12 =_se[timeid12Xdist500] ///
	se_13 =_se[timeid13Xdist500] se_14 =_se[timeid14Xdist500] , ///
	saving("$datadir\loansprop_500.dta", replace): ///
	reghdfe ln_num_app timeid*Xdist500 if $filters, a($fe $dist_controls##i.time_id i.state_id#i.time_id) cluster(district_id)

	
	cap drop mobile_rate
	cap drop bank_rate
	merge 1:1 district state time using "$datadir\districts\district_bank_mobile.dta", keep(match) nogenerate 
	
	replace mobile_rate = log(1+mobile_rate)
	replace bank_rate = log(1+bank_rate)
	
	**** Event study: Mobile
	statsby eff_1=_b[timeid1Xdist500] eff_2=_b[timeid2Xdist500] eff_3=_b[timeid3Xdist500] eff_4=_b[timeid4Xdist500] ///
	eff_5=_b[timeid5Xdist500] eff_6=_b[timeid6Xdist500] eff_7=_b[timeid7Xdist500] eff_8 =_b[timeid8Xdist500] ///
	eff_9 =_b[timeid9Xdist500] eff_10 =_b[timeid10Xdist500] eff_11=_b[timeid11Xdist500] eff_12 =_b[timeid12Xdist500] ///
	eff_13 =_b[timeid13Xdist500] eff_14 =_b[timeid14Xdist500] se_1 =_se[timeid1Xdist500] se_2 =_se[timeid2Xdist500] ///
	se_3 = _se[timeid3Xdist500] se_4 =_se[timeid4Xdist500] se_5 =_se[timeid5Xdist500] se_6 =_se[timeid6Xdist500] se_7 =_se[timeid7Xdist500] ///
	se_8 =_se[timeid8Xdist500] se_9 =_se[timeid9Xdist500] se_10 =_se[timeid10Xdist500] se_11 =_se[timeid11Xdist500] se_12 =_se[timeid12Xdist500] ///
	se_13 =_se[timeid13Xdist500] se_14 =_se[timeid14Xdist500] , ///
	saving("$datadir\mobilerate_500.dta", replace): ///
	reghdfe mobile_rate timeid*Xdist500 if $filters, a($fe $dist_controls##i.time_id i.state_id#i.time_id) cluster(district_id)
	
	
	**** Event study: Banks
	statsby eff_1=_b[timeid1Xdist500] eff_2=_b[timeid2Xdist500] eff_3=_b[timeid3Xdist500] eff_4=_b[timeid4Xdist500] ///
	eff_5=_b[timeid5Xdist500] eff_6=_b[timeid6Xdist500] eff_7=_b[timeid7Xdist500] eff_8 =_b[timeid8Xdist500] ///
	eff_9 =_b[timeid9Xdist500] eff_10 =_b[timeid10Xdist500] eff_11=_b[timeid11Xdist500] eff_12 =_b[timeid12Xdist500] ///
	eff_13 =_b[timeid13Xdist500] eff_14 =_b[timeid14Xdist500] se_1 =_se[timeid1Xdist500] se_2 =_se[timeid2Xdist500] ///
	se_3 = _se[timeid3Xdist500] se_4 =_se[timeid4Xdist500] se_5 =_se[timeid5Xdist500] se_6 =_se[timeid6Xdist500] se_7 =_se[timeid7Xdist500] ///
	se_8 =_se[timeid8Xdist500] se_9 =_se[timeid9Xdist500] se_10 =_se[timeid10Xdist500] se_11 =_se[timeid11Xdist500] se_12 =_se[timeid12Xdist500] ///
	se_13 =_se[timeid13Xdist500] se_14 =_se[timeid14Xdist500] , ///
	saving("$datadir\bankrate_500.dta", replace): ///
	reghdfe bank_rate timeid*Xdist500 if $filters, a($fe $dist_controls##i.time_id i.state_id#i.time_id) cluster(district_id)
	


******************************************************************	
** Event study figures : Loans, Banks, Mobile phones
******************************************************************	

use "$datadir\mobwalletprop_500.dta",clear
	  gen i=1
	  reshape long eff_ se_ , i(i) j(period)
	  drop i
	  rename eff_ eff
	  rename se_ se
	  tset period
	  gen t_10=1.65
	  gen t_5=1.96
	  
	  foreach num in 5 10 {
	  gen low_bound_`num'=eff-(t_`num'*se)
	  gen up_bound_`num'=eff+(t_`num'*se)
	 }
	 replace period = period-6
	 
	*  ylab(-3(1)1)
	twoway (rcap up_bound_10 low_bound_10 period, lcolor(gray) lstyle(ci)) (scatter eff period, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(off) graphregion(color(white)) xline(2, lwidth(26) lc(gs12)) ylab(-1(0.25)0.25,nogrid) xlab(-5(1)8) yline(0, lcolor(gs10) lpattern(dash)) ///
	xtitle(" " "Month from event") ytitle("Effect on new firms" " ") xline(0.5, lcolor(red) lpattern(dash))
	graph export "$outputdir_fig\Figure_H_10_1.pdf", as(pdf) replace
	erase "$datadir\mobwalletprop_500.dta"

use "$datadir\loansprop_500.dta",clear
	  gen i=1
	  reshape long eff_ se_ , i(i) j(period)
	  drop i
	  rename eff_ eff
	  rename se_ se
	  tset period
	  gen t_10=1.65
	  gen t_5=1.96
	  
	  foreach num in 5 10 {
	  gen low_bound_`num'=eff-(t_`num'*se)
	  gen up_bound_`num'=eff+(t_`num'*se)
	 }
	 replace period = period-6
	 
	*  ylab(-3(1)1)
	twoway (rcap up_bound_10 low_bound_10 period, lcolor(gray) lstyle(ci)) (scatter eff period, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(off) graphregion(color(white)) xline(2, lwidth(26) lc(gs12)) ylab(-1(0.25)0.25,nogrid) xlab(-5(1)8) yline(0, lcolor(gs10) lpattern(dash)) ///
	xtitle(" " "Month from event") ytitle("Effect on loan applications" " ") xline(0.5, lcolor(red) lpattern(dash))
	graph export "$outputdir_fig\Figure_H_10_2.pdf", as(pdf) replace
	erase "$datadir\loansprop_500.dta"

use "$datadir\mobilerate_500.dta",clear
	  gen i=1
	  reshape long eff_ se_ , i(i) j(period)
	  drop i
	  rename eff_ eff
	  rename se_ se
	  tset period
	  gen t_10=1.65
	  gen t_5=1.96
	  
	  foreach num in 5 10 {
	  gen low_bound_`num'=eff-(t_`num'*se)
	  gen up_bound_`num'=eff+(t_`num'*se)
	 }
	 replace period = period-6
	 *drop if period > 6
	*  ylab(-3(1)1)
	twoway (rcap up_bound_10 low_bound_10 period, lcolor(gray) lstyle(ci)) (scatter eff period, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(off) graphregion(color(white)) xline(2, lwidth(26) lc(gs12)) ylab(-0.1(0.05)0.1,nogrid) xlab(-5(1)8) yline(0, lcolor(gs10) lpattern(dash)) ///
	xtitle(" " "Month from event") ytitle("Effect on mobile ownership") xline(0.5, lcolor(red) lpattern(dash))
	graph export "$outputdir_fig\Figure_H_10_3.pdf", as(pdf) replace
	erase "$datadir\mobilerate_500.dta"


use "$datadir\bankrate_500.dta",clear
	  gen i=1
	  reshape long eff_ se_ , i(i) j(period)
	  drop i
	  rename eff_ eff
	  rename se_ se
	  tset period
	  gen t_10=1.65
	  gen t_5=1.96
	  
	  foreach num in 5 10 {
	  gen low_bound_`num'=eff-(t_`num'*se)
	  gen up_bound_`num'=eff+(t_`num'*se)
	 }
	 replace period = period-6
	 *drop if period > 6
	*  ylab(-3(1)1)
	twoway (rcap up_bound_10 low_bound_10 period, lcolor(gray) lstyle(ci)) (scatter eff period, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(off) graphregion(color(white)) xline(2, lwidth(26) lc(gs12)) ylab(-0.1(0.05)0.1,nogrid) /*ylab(-1(0.25)0.25,nogrid)*/ xlab(-5(1)8) yline(0, lcolor(gs10) lpattern(dash)) ///
	xtitle(" " "Month from event") ytitle("Effect on banking ownership" " ") xline(0.5, lcolor(red)  lpattern(dash))
	graph export "$outputdir_fig\Figure_H_10_4.pdf", as(pdf) replace
	erase "$datadir\bankrate_500.dta"


		